package com.brt.socket.rest.controller.socketConfig;


import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

/**
 * websocket 服务端的IO事件处理
 * @author yuansili
 * */
public class IOHandler implements WebSocketHandler{
	
	private Logger logger = Logger.getLogger(getClass());
	
	@Override
	public void afterConnectionClosed(WebSocketSession session, CloseStatus status)throws Exception {
		SockJSServer.users.remove(session);
		logger.info("======>当前IP : " + session.getRemoteAddress().getAddress().getHostAddress() + "已断开连接,id : " + session.getId());
		logger.info("======>当前内存中存在的会话 :");
		logger.info("ID      登陆名            IP       开始连接时间");
		logger.info("====================================================================");
		for(WebSocketSession webSocketSession : SockJSServer.users){
			if(webSocketSession.isOpen()){
				logger.info(webSocketSession.getId() + "     " + webSocketSession.getPrincipal().getName()+ "    " + webSocketSession.getRemoteAddress().getAddress().getHostAddress() +"       "+ new Timestamp((long)session.getAttributes().get("connectTime")).toString());
			}else{
				logger.info("清除失效链接,id : " + webSocketSession.getId());
				webSocketSession.close();
			}
		}
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session)throws Exception {
		String remoteIp = session.getRemoteAddress().getAddress().getHostAddress();
		
		logger.info("======>当前Ip："+ remoteIp + "成功与服务端创建webSocket连接 通道！");
		
		
		//记录连接时间
		session.getAttributes().put("connectTime",System.currentTimeMillis());
		
		SockJSServer.users.add(session);
			
		logger.info("======>当前内存中存在的会话 :");
		logger.info("ID      登陆名            IP       开始连接时间");
		logger.info("====================================================================");
		for(WebSocketSession webSocketSession : SockJSServer.users){
			if(webSocketSession.isOpen()){
				logger.info(webSocketSession.getId() + "     " + webSocketSession.getPrincipal().getName()+ "    " + webSocketSession.getRemoteAddress().getAddress().getHostAddress() +"       "+ new Timestamp((long)session.getAttributes().get("connectTime")).toString());
			}else{
				logger.info("清除失效链接,id : " + webSocketSession.getId());
				webSocketSession.close();
			}
		}
	}
	/**
	 * 初始连接时，接受客户端座席号，并存入sockjsSession
	 */
	@Override
	public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
			throws Exception {
		
	}

	@Override
	public void handleTransportError(WebSocketSession session, Throwable throwable)
			throws Exception {
		/*if(session.isOpen()){
			session.close();
		}*/
		logger.info("======>TransportError websocket closed!");
		SockJSServer.users.remove(session);
	}

	@Override
	public boolean supportsPartialMessages() {
		// TODO Auto-generated method stub
		return false;
	}
	
	

}
